package com.wn.course.view

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.wn.common.widget.BindingViewHolder
import com.wn.course.databinding.ItemCourseRecycBinding
import com.wn.course.repository.data.Daily
import com.wn.course.view.playvideo.PlayVideoActivity
import dagger.hilt.android.qualifiers.ActivityContext
import dagger.hilt.android.scopes.ActivityScoped
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2023/10/19 17:20
 * @Description: 课程中心 paging3 适配器
 */
const val VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"

@ActivityScoped
class CourseAdapter @Inject constructor(
  @ActivityContext private val context: Context,
  private val playVideoActivity: PlayVideoActivity,
) :
  PagingDataAdapter<Daily.Item, BindingViewHolder>(myDiff) {

  override fun onBindViewHolder(holder: BindingViewHolder, position: Int) {
    val item = getItem(position)
    item?.let {
      val binding = holder.binding as ItemCourseRecycBinding
      binding.info = it
      // holder.itemView  列表项
      // 取图片做点击事件
      binding.ivBadgeItemStudy.setOnClickListener { _ ->
        // 点击执行 跳转 到 视频页
        playVideoActivity.openPlayVideo(context, VIDEO_URL, it)
      }
    }
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {
    val binding = ItemCourseRecycBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return BindingViewHolder(binding)
  }
}

// diff 比较
private val myDiff = object : DiffUtil.ItemCallback<Daily.Item>() {
  override fun areItemsTheSame(oldItem: Daily.Item, newItem: Daily.Item): Boolean =
    oldItem.id == newItem.id

  override fun areContentsTheSame(oldItem: Daily.Item, newItem: Daily.Item): Boolean =
    oldItem == newItem
}